package fx

func DefaultKeyWordParams[D any](defVal D, params ...D) D {
	if len(params) == 0 {
		return defVal
	}
	return params[0]
}

// 快排
func QuickSort[T any](arr []T, compareFn func(a, b T) bool) {
	if len(arr) < 2 {
		return
	}

	pivot := arr[0]
	left := 1
	right := len(arr) - 1

	for left <= right {
		if compareFn(arr[left], pivot) {
			left++
		} else if compareFn(pivot, arr[right]) {
			right--
		} else {
			arr[left], arr[right] = arr[right], arr[left]
		}
	}

	arr[0], arr[right] = arr[right], arr[0]

	QuickSort(arr[:right], compareFn)
	QuickSort(arr[right+1:], compareFn)
}

type Student struct {
	Name string
	Age  int
}

func NewStudent(name string, age int) *Student {
	return &Student{
		Name: name,
		Age:  age,
	}
}

func DefaultFilter(item interface{}) (uniqueKey interface{}) {
	return item.(*Student).Name
}

func RemoveDuplicateWithFilter[T comparable](compareSlice []T, filterFunc func(item interface{}) (key interface{})) []T {
	set := map[interface{}]struct{}{}
	res := []T{}

	for _, item := range compareSlice {
		i := filterFunc(item)
		if _, ok := set[i]; !ok {
			res = append(res, item)
			set[i] = struct{}{}
		}
	}

	return res
}
